<?php

/**
 * Created by EPP.
 * User: yuxun
 * Date: 18/6/26
 * Time: 21:21
 */
class CodePoolService extends Service{

    public static function indexHeader(){
        $header = array(
        		'id'           =>array('text' => 'ID', 'width' => '50px'),
        		'classify'  => array('text' => '种类', 'width' => '100px'),
        		'bag_id'     => array('text' => '礼包', 'width' => '70px'),
                'code_num'     => array('text' => '数量', 'width' => '70px'),
        		'channel_list' => array('text' => '渠道', 'width' => '300px'),
        		'is_same_accoun' => array('text' => '同账号可领取', 'width' => '100px'),
        		'is_same_role' => array('text' => '同角色可领取', 'width' => '100px'),
        		'limit_time' => array('text' => '重复领取时间限制', 'width' => '120px'),
        		'limit_grade' => array('text' => '领取等级限制', 'width' => '100px'),
        		
        		'start_time'   => array('text' => '生效时间', 'width' => '100px'),
        		'end_time'     => array('text' => '失效时间', 'width' => '100px'),
        		'status'     => array('text' => '状态', 'width' => '100px'),
        		'desc'     => array('text' => '描述', 'width' => '200px'),
        		'create_time'  => array('text' => '创建时间', 'width' => '100px'),
                'action'       => array('text' => '操作', 'width' => '150px'),
        );
        return $header;
    }

    public static function indexData($params){
        $modCodePool = new ModCodePool();
        $result = $modCodePool->page($params);
        
        $classifyList = GiftBagService::classifyList();
        $statusList = self::statusList();
        $isSameList = self::isSameList();
        foreach($result['data'] as &$v){
        	$v['classify'] = $classifyList[$v['classify']];
        	$v['status'] = $statusList[$v['status']];
        	$v['is_same_accoun'] = $isSameList[$v['is_same_accoun']];
        	$v['is_same_role'] = $isSameList[$v['is_same_role']];
        	
        	$limit_time = $v['limit_time'] == 0? '无限制' : $v['limit_time'];
        	$v['limit_time'] = $limit_time;
        	
        	$limit_grade = $v['limit_grade'] == 0? '无限制' : $v['limit_grade'];
        	$v['limit_grade'] = $limit_grade;
        	
        	$start_time = $v['start_time'] == 0? '无限制' : date('Y-m-d H:i', $v['start_time']);
        	$v['start_time'] = $start_time;
        	
        	$end_time = $v['end_time'] == 0? '无限制' : date('Y-m-d H:i', $v['end_time']);
        	$v['end_time'] = $end_time;
        	
        	if($v['is_all_channel'] == 1){
        		$v['channel_list'] = self::getChannel($v['id']);
        		$v['channel_list'] = '<div style="white-space:normal;">'.$v['channel_list'].'</div>';
        	}
        	if($v['is_all_channel'] == 2){
        		$v['channel_list'] = '全渠道';
        	}
        	

        	$v['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
        	$id = $v['id'];
        	$v['action']         = ' <a href="javascript:;" name="code_view" data-id="'.$id.'">激活码</a>&nbsp;&nbsp;';
        	$v['action']        .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=codePool/edit').'&id='.$v['id'].'">编辑</a>&nbsp;&nbsp;';
        	$v['action']         .= ' <a href="javascript:;" name="export" data-id="'.$id.'">导出</a>&nbsp;&nbsp;';
        	//$v['action']         .= ' <a href="'.PubFunc::buildUrl('/oss/?r=codePool/delete').'&id='.$id.'" confirm="是否要删除该条信息?">删除</a>';
        }
        unset($v);

        return $result;
    }
    
    public static function getChannel($code_pool_id){
    	$modCodePoolChannel = new ModCodePoolChannel();
    	$channelData = $modCodePoolChannel->selectChannel($code_pool_id);
    	$list = '';
    	foreach ($channelData as $key=>$val){
    		$list .= ','.$val['channel_name'];
    	}
    	$list = substr($list, 1);
    	return $list;
    }
    
    public static function statusList(){
    	$list = array(
    			'1'	 => '启用',
    			'2'  => '关闭',
    	);
    	return $list;
    }
    
    public static function isSameList(){
    	$list = array(
    			'1'	 => '不可以',
    			'2'  => '可以',
    	);
    	return $list;
    }
    
    public static function isUseList(){
    	$list = array(
    			'1'	 => '未使用',
    			'2'  => '已使用',
    			'3'  => '通用码',
    	);
    	return $list;
    }
    
    public static function doAdd($params){
    	if(PubFunc::checkEmpty($params, array('classify', 'bag_id','code_num'))){
    		return array('state' => false, 'msg' => '缺少必要参数.');
    	}
    	
    	$is_same_accoun = $params['is_same_accoun'];
    	if($is_same_accoun == 1){ //不可以
    		$is_same_role = 1;
    		$limit_time = 0;
    	}
    	if($is_same_accoun == 2){ //可以
    		$is_same_role = $params['is_same_role'];
    		$limit_time = $params['limit_time'];
    	}
    	
    	if($limit_time == ''){
    		$limit_time = 0;
    	}
    	
    	$limit_grade = $params['limit_grade'];
    	if($limit_grade == '' ){
    		$limit_grade = 0;
    	}
    	
    	$start_time = $params['start_time'];
    	if($start_time != ''){
    		$start_time = strtotime($start_time);
    	}else if($start_time == ''){
    		$start_time = 0;
    	}
    	
    	$end_time = $params['end_time'];
    	if($end_time != ''){
    		$end_time = strtotime($end_time);
    	}else if($end_time == ''){
    		$end_time = 0;
    	}

    	$user = AccAuth::me();
    	$modCodePool = new ModCodePool();
    	
    	$modCodePool->platform      = 'test';
    	$modCodePool->game          = $params['g'];
    	$modCodePool->bag_id        = $params['bag_id'];
    	$modCodePool->classify     = $params['classify'];
    	$modCodePool->code_num      = $params['code_num'];
    	$modCodePool->code_key      = $params['code_key'];
    	$modCodePool->desc      = $params['desc'];
    	
    	$modCodePool->is_same_accoun = $is_same_accoun;
    	$modCodePool->is_same_role = $is_same_role;
    	$modCodePool->limit_time = $limit_time;
    	$modCodePool->limit_grade      = $limit_grade;
    	$modCodePool->start_time =$start_time;
    	$modCodePool->end_time =$end_time;
    	
    	$modCodePool->create_user   = $user['UserID'];
    	$modCodePool->create_time   = time();
    	$modCodePool->update_user   = $user['UserID'];
    	$modCodePool->update_time   = time();
    	$modCodePool->status = $params['status'];
    	$modCodePool->is_all_channel = $params['is_all_channel'];
    	$result = $modCodePool->save();
    	if($result){
    		$pool_id = $modCodePool->id;
    		//保存码池与渠道的关系
    		if($params['is_all_channel'] == 1){ //全渠道不用保存，2为全渠道
    			$channel_id_array = $params['channel_id'];
    			$channel_code_pool = array();
    			foreach ($channel_id_array as $key=>$val){
    				$data = array();
    				$data['code_pool_id'] = $pool_id;
    				$data['channel_id'] = $val;
    				array_push($channel_code_pool, $data);
    			}
    			$modCodePoolChannel = new ModCodePoolChannel();
    			$result = $modCodePoolChannel->multiAdd($channel_code_pool);
    		}
    		
    		
    		//生成激活码
    		$pool_item = array();
    		$is_use = 1;
    		if($params['classify'] == 2){
    			$is_use = 3;  //3一码多用 
    		}
    		for($i=0;$i<$params['code_num'];$i++){
    			$data = array();
    			$data['pool_id'] = $pool_id;
    			$data['code_number'] = $params['code_key'].self::random(8,'all');
    			$data['is_use'] = $is_use;
    			$data['classify'] = $params['classify'];
    			array_push($pool_item, $data);
    		}
    		$modCode = new ModCode();
    		$result = $modCode->multiAdd($pool_item);
    		return true;
    	}else{
    		return false;
    	}	
    
    }
    
    public static function get($id){
    	$modCodePool = new ModCodePool();
    	$obj = $modCodePool->findByPk($id);
    
    	if($obj){
    		$info = $obj->getAttributes();   
    		return $info;
    	}else{
    		return false;
    	}
    }
    
    public static function doEdit($id, $params){
    	if(empty($id)) return false;
    	$user = AccAuth::me();
    	
    	$is_same_accoun = $params['is_same_accoun'];
    	if($is_same_accoun == 1){ //不可以
    		$is_same_role = 1;
    		$limit_time = 0;
    	}
    	if($is_same_accoun == 2){ //可以
    		$is_same_role = $params['is_same_role'];
    		$limit_time = $params['limit_time'];
    	}
    	
    	 
    	if($limit_time == ''){
    		$limit_time = 0;
    	}
    	 
    	$limit_grade = $params['limit_grade'];
    	if($limit_grade == '' ){
    		$limit_grade = 0;
    	}
    	 
    	$start_time = $params['start_time'];
    	if($start_time != ''){
    		$start_time = strtotime($start_time);
    	}else if($start_time == ''){
    		$start_time = 0;
    	}
    	 
    	$end_time = $params['end_time'];
    	if($end_time != ''){
    		$end_time = strtotime($end_time);
    	}else if($end_time == ''){
    		$end_time = 0;
    	}
    
    	$modCodePool = new ModCodePool();
    	$update = array();

    	$update['bag_id'] = $params['bag_id'];
    	$update['desc'] = $params['desc'];
    	$update['is_same_accoun'] = $is_same_accoun;
    	$update['is_same_role'] = $is_same_role;
    	$update['limit_time'] = $limit_time;
    	$update['limit_grade'] = $limit_grade;
    	$update['start_time'] = $start_time;
    	$update['end_time'] = $end_time;
    	$update['status'] = $params['status'];
    	$update['update_user']  = $user['UserID'];
    	$update['update_time']  = time();
    	$update['is_all_channel'] = $params['is_all_channel'];
    	$result = $modCodePool->updateByPk($id, $update);
//     	if($result !== false){
//     		//修改渠道之间的关系，先删除，在添加
//     		$modCodePoolChannel = new ModCodePoolChannel();
//     		$modCodePoolChannel->deleteAll('code_pool_id=:code_pool_id',array(':code_pool_id'=>$id));
    		
//     		//保存码池与渠道的关系
//     		$channel_id_array = $params['channel_id'];
//     		$channel_code_pool = array();
//     		foreach ($channel_id_array as $key=>$val){
//     			$data = array();
//     			$data['code_pool_id'] = $id;
//     			$data['channel_id'] = $val;
//     			array_push($channel_code_pool, $data);
//     		}
//     		$modCodePoolChannel = new ModCodePoolChannel();
//     		$result = $modCodePoolChannel->multiAdd($channel_code_pool);    		
//     	}

    	return true;
    }
    
    public static function delete($id){
    	if(empty($id)) return false;
        //先删除码库的数据，在删除码池
    	$modCode = new ModCode();
    	$count = $modCode->deleteAll('pool_id=:pool_id',array(':pool_id'=>$id));
    	if($count !== false){
    		//删除与渠道关系的数据
    		$modCodePoolChannel = new ModCodePoolChannel();
    		$modCodePoolChannel->deleteAll('code_pool_id=:code_pool_id',array(':code_pool_id'=>$id));
    		
    		//删除码池
    		$modCodePool = new ModCodePool();
    		$modCodePool->deleteByPk($id);
    		return true;
    	}else{
    		return false;
    	}
    	
    	
    }

    /**
     * 随机字符
     * @param int $length 长度
     * @param string $type 类型
     * @param int $convert 转换大小写 1大写 0小写
     * @return string
     */
    public static function random($length=8, $type='letter', $convert=0){
    	$config = array(
    			'number'=>'1234567890',
    			'letter'=>'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
    			'string'=>'abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789',
    			'all'=>'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
    	);
    
    	if(!isset($config[$type])) $type = 'letter';
    	$string = $config[$type];
    
    	$code = '';
    	$strlen = strlen($string) -1;
    	for($i = 0; $i < $length; $i++){
    		$code .= $string{mt_rand(0, $strlen)};
    	}
    	if(!empty($convert)){
    		$code = ($convert > 0)? strtoupper($code) : strtolower($code);
    	}
    	return $code;
    }
    
    
    public static function getBag($classify){
    	if(empty($classify)){
    		$result = array('status'=>0,'msg'=>'非法参数','data'=>'');
    		return $result;
    	}
    	
    	$bagList = GiftBagService::getListGiftBag($game, 'id',$classify);
    	$result = array('status'=>1,'msg'=>'查询成功','data'=>$bagList);
    	return $result;
    	
    }

    


}